# coding:utf8

from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StringType, IntegerType
import pandas as pd

# 8顾客用户会员占比
if __name__ == '__main__':
    # 0. 构建执行环境入口对象SparkSession
    spark = SparkSession.builder.\
        appName("test").\
        getOrCreate()
    sc = spark.sparkContext

    # JSON类型自带有Schema信息
    df = spark.read.format("json").load("hdfs://node1:8020/input/mini.json")
    df.createTempView("taobao")
    spark.sql("""
                SELECT serverSaved, count(*)AS n FROM taobao  GROUP BY serverSaved ORDER BY n 
            """).write.mode("overwrite"). \
        format("jdbc"). \
        option("url", "jdbc:mysql://node1:3306/bigdata?useSSL=false&useUnicode=true"). \
        option("dbtable", "huirate"). \
        option("user", "root"). \
        option("password", "123456"). \
        save()